package com.matt.controller.web;

import com.matt.commons.exception.BusinessException;
import com.matt.commons.model.StatusResponse;
import com.matt.foundation.dto.AreaSettingDTO;
import com.matt.foundation.model.ErrorCode;
import com.matt.foundation.model.MemVerificationCache;
import com.matt.service.remoteCall.DTO.AreaListDTO;
import com.matt.service.remoteCall.SsoCallService;
import io.swagger.annotations.*;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.matt.foundation.constant.RequestConstant.TICKET;

/**
 * *******************************
 * Created by Ron.
 * form com.matt
 * on 2017/8/1
 * *******************************
 */
@RestController
@RequestMapping("/web/area")
@Api(tags ="WEB - 园区")

public class WebAreaController {
    @Autowired
    private SsoCallService ssoCallService;

    @Autowired
    private MemVerificationCache memVerificationCache;

    @ApiOperation(value = "WEB 所有关联园区",httpMethod = "POST")
    @ApiImplicitParams({
    	@ApiImplicitParam(paramType = "header", name = "ticket", value = "票据", required = true, dataType = "string"),
    })
    @ApiResponses({
  		@ApiResponse(code = 000000, message = "成功"),
  		@ApiResponse(code = 000001, message = "ticket已过期")
  	})
    @RequestMapping("/all")
    public StatusResponse list(@RequestHeader(TICKET) String ticket) {
        List<AreaListDTO> areaList = ssoCallService.getInChargeArea(ticket);
        return StatusResponse.success(areaList);
    }

    public static final String AREA_CONFIG_PREFIX = "TEACH_AREA";

    @RequestMapping(value = "/area_configs", method = RequestMethod.GET)
    @ResponseBody
    public StatusResponse getAllConfig(@RequestHeader(TICKET) String ticket){
        String areaId = memVerificationCache.getAreaId(ticket);
        List<AreaSettingDTO> s = ssoCallService.getAllAreaSettings(areaId, AREA_CONFIG_PREFIX);

        return StatusResponse.success(s);
    }

    @RequestMapping(value = "/area_configs", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "ticket", value = "票据", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "key", value = "key, 多个值请用|分隔", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "value", value = "值, 多个值请用|分隔", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "description", value = "设置描述, 多个值请用|分隔", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "extension", value = "设置扩展, 多个值请用|分隔", required = true, dataType = "string"),
    })
    @ResponseBody
    public StatusResponse setConfig(@RequestHeader(TICKET) String ticket, String key, String value, String description, String extension){

        if (StringUtils.isEmpty(key)){
            throw new BusinessException(ErrorCode.ERROR, "参数错误");
        }

        String[] keys = key.split("\\|");
        for(int i = 0; i < keys.length; i++){
            String k = keys[i];

            if (!k.startsWith(AREA_CONFIG_PREFIX)){
                keys[i] = AREA_CONFIG_PREFIX + "_" + k;
            }
        }

        key = String.join("|", keys);

        String areaId = memVerificationCache.getAreaId(ticket);

        Integer ret = ssoCallService.addAreaSettings(key, value, description, extension, areaId);

        return StatusResponse.success(ret);
    }
}
